package kr.hkit.androidcomplete02.XMLparser;

import java.io.FileOutputStream;

import kr.hkit.androidcomplete02.R;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

public class MainXml extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.network_mainxml);
		new XmlJsonMake().execute();
	}

	public void mOnClick(View v) {

		switch (v.getId()) {

		case R.id.dom:
			startActivity(new Intent(this, Dom_parserAcitivity.class));
			break;
		case R.id.sax:
			startActivity(new Intent(this, Sax_parserAcitivity.class));
			break;
		case R.id.pull:
			startActivity(new Intent(this, Pull_parserAcitivity.class));
			break;
		case R.id.json:
			startActivity(new Intent(this, Json_parserAcitivity.class));
			break;
		 
		}
	}

	private class XmlJsonMake extends android.os.AsyncTask<Void, Void, Void> {

		ProgressDialog progressDlg;

		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			progressDlg = ProgressDialog.show(MainXml.this, "Wait",
					"order.xml & order.json Making ...");
		}

		@Override
		protected void onPostExecute(Void result) {
			super.onPostExecute(result);
			progressDlg.dismiss();
			Toast.makeText(MainXml.this, "order.xml & order.json make",
					Toast.LENGTH_LONG).show();
		}

		@Override
		protected Void doInBackground(Void... params) {
			FileOutputStream fos = null;
			try {
				fos = openFileOutput("order.xml", MODE_PRIVATE);
				String str = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
						+ "<order>\n"
						+ " <item Maker=\"Samsung\" Price=\"23000\">Mouse</item>\n"
						+ " <item Maker=\"LG\" Price=\"12000\">KeyBoard</item>\n"
						+ " <item Price=\"156000\" Maker=\"Western Digital\">HDD</item>\n"
						+ "</order>";
				fos.write(str.getBytes());
				fos.close();
				fos = openFileOutput("order.json", MODE_PRIVATE);
				String Json = "[\n"
						+ "{\"Product\":\"Mouse\", \"Maker\":\"Samsung\", \"Price\":23000},\n"
						+ "{\"Product\":\"KeyBoard\", \"Maker\":\"LG\", \"Price\":12000},\n"
						+ "{\"Product\":\"HDD\", \"Maker\":\"Western Digital\", \"Price\":156000} \n"
						+ "]";
				fos.write(Json.getBytes());
				fos.close();
				Thread.sleep(3000);
			} catch (Exception e) {
				Log.d("XmlParser", e.getMessage());
			}
			return null;
		}
	}
}
